<template>
  <el-dialog
    v-model="dialogFormVisible"
    :close-on-click-modal="false"
    :title="title"
    width="460px"
    @close="close"
  >
    <el-form ref="formRef" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="客服名称：" prop="name">
        <el-input
          v-model.trim="form.name"
          clearable
          placeholder="请输入客服名称"
        />
      </el-form-item>
      <el-form-item label="客服头像：" prop="media_id">
        <div class="file_pot" style="width: 100%">
          <el-upload
            ref="upload"
            accept="image/jpeg,image/gif,image/png"
            :action="action"
            :auto-upload="false"
            :class="[
              'upload-content',
              fileList.length == 0 ? 'upload_show' : 'upload_hide',
            ]"
            :file-list="fileList"
            :limit="limit"
            :list-type="picture"
            :multiple="true"
            :on-change="handleChange"
            :on-exceed="handleExceed"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
          >
            <template #trigger v-if="fileList.length == 0">
              <vab-icon class="avatar-uploader-icon" icon="add-line" />
            </template>
            <el-dialog v-model="dialogVisible" append-to-body title="查看大图">
              <div class="upload-image">
                <el-image :src="dialogImageUrl" />
              </div>
            </el-dialog>
          </el-upload>
        </div>
      </el-form-item>
      <!-- <el-form-item label="复制客服：" prop="copy_open_kfid">
        <el-select
          v-model="form.copy_open_kfid"
          placeholder="请选择频道类型"
          style="flex-grow: 1"
        >
          <el-option
            v-for="item in kfList"
            :key="item.open_kfid"
            :label="item.name"
            :value="item.open_kfid"
          />
        </el-select>
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { addWeCom, uploadWeCom } from '@/api/enterpriseWeChat/weCom'
  export default defineComponent({
    name: 'TableEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        formRef: null,
        form: {
          name: '',
          media_id: '',
          corp_id: '',
          // copy_open_kfid: '',
        },
        rules: {
          name: [
            { required: true, trigger: 'blur', message: '请输入客服名称' },
          ],
          media_id: [
            { required: true, trigger: 'blur', message: '请选择客服头像' },
          ],
        },
        limit: 1,
        size: 1,
        picture: 'picture',
        title: '',
        action: '',
        kfList: [],
        fileList: [],
        dialogImageUrl: [],
        dialogVisible: false,
        dialogFormVisible: false,
      })
      const handlePreview = (file) => {
        state.dialogImageUrl = file.url
        state.dialogVisible = true
      }
      const handleChange = (file) => {
        if (file.size > 1048576 * state.size) {
          $baseMessage('图片不能超过1MB', 'error', 'vab-hey-error-success')
          state.fileList = []
        } else {
          const formData = new FormData()
          formData.append('corp_id', state.form.corp_id)
          formData.append('file', file.raw)
          uploadData(formData)
        }
      }
      const handleExceed = () => {
        $baseMessage('每次只能上传一张哦', 'error', 'vab-hey-error-success')
      }
      const handleRemove = () => {
        state.fileList = []
        state.form.media_id = ''
        $baseMessage('移除成功', 'success', 'vab-hey-message-success')
      }
      const uploadData = async (file) => {
        state.fileList.push({ url: '', name: '查看详情' })
        const { code, message, data } = await uploadWeCom(file)
        if (code == 200) {
          state.form.media_id = data.media_id
        }
        $baseMessage(message, 'success', 'vab-hey-message-success')
      }
      const showEdit = (corp_id, list) => {
        console.log(list)
        state.kfList = list
        state.title = '添加客服'
        state.form.corp_id = corp_id
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {}
        state.fileList = []
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { message } = await addWeCom(state.form)
            $baseMessage(message, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }
      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
        handleChange,
        handlePreview,
        handleExceed,
        handleRemove,
      }
    },
  })
</script>
